import type { ID } from '#/api';

import { acceptHMRUpdate, defineStore } from 'pinia';

interface XTenant {
  xtenantId: ID | null;
  xtenantName: null | string;
}

/**
 * @zh_CN 访问租户相关
 */
export const useXTenantStore = defineStore('x-tenant', {
  actions: {
    setXTenantId(xtenantId: ID) {
      this.xtenantId = xtenantId;
    },
    setXTenantName(xtenantName: string) {
      this.xtenantName = xtenantName;
    },
  },
  persist: {
    // 持久化
    pick: ['xtenantId', 'xtenantName'],
  },
  state: (): XTenant => ({
    xtenantId: null,
    xtenantName: null,
  }),
});

// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
  hot.accept(acceptHMRUpdate(useXTenantStore, hot));
}
